package dream.mapper;

import dream.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ArticleMapper {
    /**
     * 查询用户的博客
     *
     * @param id
     * @return
     */
    @Select("select * from article where id = #{id} order by aid desc")
    List<Article> selectById(int id);


    /**
     * 根据文章id查询博客的全部内容
     *
     * @param aid
     * @return
     */
    @Select("select * from article where aid = #{aid}")
    Article selectByAid(int aid);


    /**
     * 插入一篇博客
     *
     * @param title
     * @param context
     * @param id
     */
    @Insert("insert into article(title, context, id) values(#{title}, #{context}, #{id})")
    void insertBlog(@Param("title") String title, @Param("context") String context, @Param("id") int id);

    /**
     * 查询用户信息
     *
     * @param aid
     * @return
     */
    @Select("select username from tb_user where id=(select id from article where aid=#{aid})")
    String selectUser(int aid);


    /**
     * 查询用户信息
     *
     * @param aid
     * @return
     */
    @Select("select count(*) as acount from article where id=(select id from article where aid=#{aid})")
    int selectArticleCount(int aid);


    /**
     * 修改文章
     *
     * @param aid
     */
    @Update("update article set title=#{title},context=#{context} where aid=#{aid}")
    void updateByAid(@Param("title") String title, @Param("context") String context, @Param("aid") int aid);


    /**
     * 更新阅读量
     *
     * @param aid
     */
    @Update("update article set readcount = readcount + 1 where aid=#{aid}")
    void updateReadCount(int aid);


    /**
     * 删除博客
     * @param aid
     */
    @Delete("delete from article where aid=#{aid}")
    void deleteBlog(int aid);


    /**
     * 主页分页查询
     * @param currPage
     * @param pageSize
     * @return
     */
    @Select("select * from article limit #{currPage} , #{pageSize}")
    List<Article> showBlogByPageServlet(@Param("currPage") int currPage, @Param("pageSize")int pageSize);


    /**
     * 查询文章点赞情况
     * @param id
     * @param aid
     * @return
     */
    @Select("select count(*) from good where id=#{id} and aid=#{aid}")
    int selectGood(@Param("id") int id, @Param("aid") int aid);


    /**
     * 查询文章的收藏情况
     * @param id
     * @param aid
     * @return
     */
    @Select(("select count(*) from collect where id=#{id} and aid=#{aid}"))
    int selectYes(@Param("id") int id, @Param("aid") int aid);


    /**
     * 点赞数加一
     * @param aid
     */
    @Update("update article set count = count + 1 where aid=#{aid}")
    void addGoodCount(int aid);


    /**
     * 建立点赞功能用户与文章之间的关系
     * @param id
     * @param aid
     */
    @Insert("insert into good set id=#{id}, aid=#{aid}")
    void insertGood(@Param("id") int id, @Param("aid") int aid);


    /**
     * 建立收藏功能用户与文章之间的关系
     * @param id
     * @param aid
     */
    @Insert("insert into collect set id=#{id}, aid=#{aid}")
    void insertYes(@Param("id") int id, @Param("aid") int aid);


    /**
     * 查询文章的点赞数
     * @param aid
     * @return
     */
    @Select("select count from article where aid= #{aid}")
    int selectGoodCount(int aid);


    /**
     * 搜索的模糊查询
     * @param val
     * @return
     */
    @Select("select * from article where title like CONCAT('%',#{val},'%') or context like CONCAT('%',#{val},'%')")
    List<Article> selectByVal(String val);


    /**
     * 查询用户收藏的文章
     * @param id
     * @return
     */
    @Select("select aid from collect where id = #{id}")
    int[] selectCollectById(int id);
}
